const App = getApp()

Page({
  data: {
    logged: !1,
    signInData: {},
    phoneNumber: '',
    selectedStation: {},
    verifyButton: {
      verifyText: '获取验证码',
      disabled: false
    },
    verifyCode: ''
  },

  onLoad() {
    let [signInData, selectedStation] = App.getAllStorageSyncs('signIn selectedStation');
    this.sendSMS = App.HttpResource('/users/send_sms')
    this.setData({
      signInData: signInData,
      selectedStation: selectedStation
    })
  },

  onShow() {
    const token = App.WxService.getStorageSync('token')
    this.setData({
      logged: !!token
    })
    token && setTimeout(this.reject, 20)
  },

  verifyCodeHandle(event) {
    this.setData({
      verifyCode: event.detail.value
    })
  },

  verifyStatusChange(wait) {
    if (wait == 0) {
      this.setData({
        verifyButton: {
          verifyText: '获取验证码',
          disabled: false
        }
      })
    } else {
      this.setData({
        verifyButton: {
          verifyText: "重新发送(" + wait + ")",
          disabled: true
        }
      })
      wait -= 1
      setTimeout(() => this.verifyStatusChange(wait), 1000)
    }
  },

  getVerifyNumber(event) {
    if (!this.data.phoneNumber || this.data.phoneNumber.length != 11) {
      App.WxService.showModal({
        title: '友情提示',
        content: '请先输入正确的手机号'
      })
      return
    }
    this.sendSMS.queryAsync({
        phone_number: this.data.phoneNumber
      })
      .then(data => {
        let wait = 60
        this.verifyStatusChange(wait)
      })
  },

  login() {
    if (!this.data.phoneNumber) {
      App.WxService.showModal({
        title: '友情提示',
        content: '请先输入正确的手机号',
        showCancel: !1,
      })
      return
    }
    this.wechatSignIn()
  },

  reject() {
    App.WxService.switchTab({
      url: '/pages/index/index'
    })
  },

  updatePhone: function(event) {
    this.setData({
      phoneNumber: event.detail.value
    })
  },

  showModal(content) {
    App.WxService.showModal({
      title: '友情提示',
      content: content || '登录失败，请重新登录'
    })
    return
  },

  getUserInfo(e) {
    if (!this.data.phoneNumber) {
      App.WxService.showModal({
        title: '友情提示',
        content: '请先输入正确的手机号',
        showCancel: !1,
      })
      return
    }

    if (App.WxService.getStorageSync('token'))
      return

    let userProfile = e.detail.userInfo
    console.log(userProfile)
    App.WxService.login().then(data => {
      console.log(data)
      return App.HttpService.wechatSignIn({
        code: data.code,
        station_id: this.data.selectedStation.id,
        user_profile: userProfile,
        verify_code: this.data.verifyCode,
        phone_number: this.data.phoneNumber,
        user_type: 'SELLER',
      })
    }).then(data => {
      if (data.success) {
        App.WxService.setStorageSync('token', data.value.token)
        App.WxService.setStorageSync('user', data.value.user)
        App.WxService.navigateBack()
      } else {
        this.showModal(data.msg)
      }
    })
  },

  wechatSignIn() {
    let signInData = this.data.signInData
    if (App.WxService.getStorageSync('token'))
      return
    App.WxService.getUserInfo().then(data => {
      let userProfile = data.userInfo
      App.WxService.login().then(data => {
        console.log(data)
        return App.HttpService.wechatSignIn({
          code: data.code,
          station_id: this.data.selectedStation.id,
          user_profile: userProfile,
          verify_code: this.data.verifyCode,
          phone_number: this.data.phoneNumber,
          user_type: 'SELLER',
        })
      }).then(data => {
        if (data.success) {
          App.WxService.setStorageSync('token', data.value.token)
          App.WxService.setStorageSync('user', data.value.user)
          App.WxService.navigateBack()
        } else {
          this.showModal(data.msg)
        }
      })
    })
  }
})